import json
import pytz
from aiogram import Bot, Dispatcher, types, F
import asyncio
import aiofiles
import io
import logging
from asgiref.sync import sync_to_async, async_to_sync
from icecream import ic
from aiogram.filters import CommandStart, Command
from aiogram.types import ReplyKeyboardMarkup, KeyboardButton, ReplyKeyboardRemove, Message, ContentType, InputMediaPhoto, InputFile
from aiogram.fsm.context import FSMContext
from keyboards.my_sett_kb import options_kb, options2_kb, cancel_kb, options3_kb
from aiogram import Router

from models.model import Cities, User


router = Router()


@router.message(CommandStart())
async def start(message: Message, state: FSMContext):
    user_tg_id = message.from_user.id
    username = message.from_user.username
    await User.get_or_create(tg_id=user_tg_id, username=username)
    await message.answer('Тут будет информация о погоде по времени', reply_markup=options_kb)


async def interval_senders(callback: types.CallbackQuery, state: FSMContext):
    await callback.answer('')
    dict_ = {
        'five_minutes': 5,
        'ten_minutes': 10,
        'thirty_minutes': 30
    }
    user = await User.get(tg_id=callback.from_user.id)
    user.range = dict_[callback.data]
    await user.save()
    await callback.message.edit_text('Настройка выполнена', reply_markup=options_kb)
    
@router.callback_query()
async def filtering(callback: types.CallbackQuery, state: FSMContext):

    actions = {
        'opt_1': lambda: options_weather(callback, state),
        'citie': lambda: options_citie(callback, state),
        'range': lambda: options_range(callback, state),
        'go_back1': lambda: callback.message.edit_text('Тут будет информация о погоде по времени', reply_markup=options_kb),
        'go_back2': lambda: callback.message.edit_text('Выберите город и укажите диапазон оповещения', reply_markup=options2_kb),
        'five_minutes': lambda: interval_senders(callback, state),
        'ten_minutes': lambda: interval_senders(callback, state),
        'thirty_minutes': lambda: interval_senders(callback, state),
    }
    await actions[callback.data]()

async def options_weather(callback: types.CallbackQuery, state: FSMContext):
    await callback.message.edit_text('Выберите город и укажите диапазон оповещения', reply_markup=options2_kb)
    

async def options_citie(callback: types.CallbackQuery, state: FSMContext):
    bot_message = await callback.message.edit_text('Укажите город', reply_markup=cancel_kb)
    await state.set_data({'message_id': bot_message.message_id})


@router.message()
async def filter_mess(message: Message, state: FSMContext):
    await message.delete()
    bot_id = await state.get_data()
    await message.bot.delete_message(message.chat.id, bot_id['message_id'])
    citie = message.text
    
    user = await User.get(tg_id=message.from_user.id)
    citie, created = await Cities.get_or_create(citie=citie, defaults={'user': user, 'active': True})
    await citie.save()
    await citie.deactivate_other_cities()
    await message.answer(f'Город {citie.citie} добавлен\n<b>Укажите диапазон оповещения</b>', reply_markup=options2_kb, parse_mode='HTML')


async def options_range(callback: types.CallbackQuery, state: FSMContext):
    await callback.message.edit_text('Укажите время', reply_markup=options3_kb)